package config

import (
	"github.com/ghodss/yaml"
	"io/ioutil"
	"path/filepath"
	"runtime"
)

var (
	_, b, _, _ = runtime.Caller(0)
	// Root folder of this project
	Root = filepath.Join(filepath.Dir(b), "../..")
)

type DbConfig struct {
	Driver          string `yaml:"Driver"`
	Host            string `yaml:"Host"`
	Port            int    `yaml:"Port"`
	Dbname          string `yaml:"Dbname"`
	Charset         string `yaml:"Charset"`
	Username        string `yaml:"Username"`
	Password        string `yaml:"Password"`
	MaxOpenConns    int    `yaml:"MaxOpenConns"`
	MaxIdleConns    int    `yaml:"MaxIdleConns"`
	ConnMaxLifetime int    `yaml:"ConnMaxLifetime"`
}

type config struct {
	Rpc struct {
		RegisterName struct {
			AuthorizationAdmin string `yaml:"AuthorizationAdmin"`
			Admin              string `yaml:"Admin"`
		} `yaml:"RegisterName"`
		Address struct {
			AuthorizationAdmin string `yaml:"AuthorizationAdmin"`
			Admin              string `yaml:"Admin"`
		} `yaml:"Address"`
	} `yaml:"Rpc"`
	Db  DbConfig `yaml:"Db"`
	Log struct {
		Filename string `yaml:"Filename"`
		Level    uint   `yaml:"Level"`
	} `yaml:"Log"`
}

var Config *config

func init() {
	bytes, e := ioutil.ReadFile(Root + "/config/config.yaml")
	if e != nil {
		panic(e)
	}

	if e = yaml.Unmarshal(bytes, &Config); e != nil {
		panic(e)
	}
}
